/**@FILE:ExpressServiceImpl.java
 * @AUTHOR:qingsheng.zheng
 * @DATE:2015-1-31 下午6:32:25*/
package com.xf.toy.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xf.pub.exception.ServiceException;
import com.xf.toy.bean.Express;
import com.xf.toy.bean.ExpressExample;
import com.xf.toy.bean.ExpressExample.Criteria;
import com.xf.toy.bean.Toyorder;
import com.xf.toy.bean.Wish;
import com.xf.toy.dao.ExpressMapper;
import com.xf.toy.dao.ToyorderMapper;
import com.xf.toy.service.ExpressService;
import com.xf.toy.service.OrderService;
import com.xf.toy.service.WishService;
import com.xf.toy.service.constant.ToyConst;

/*******************************************
 * @COMPANY:alibaba
 * @CLASS:ExpressServiceImpl
 * @DESCRIPTION:
 * @AUTHOR:qingsheng.zheng
 * @VERSION:v1.0
 * @DATE:2015-1-31 下午6:32:25
 *******************************************/
@Service("expressService")
public class ExpressServiceImpl implements ExpressService {
	@Resource
	private ExpressMapper expressMapper;
	@Resource
	private ToyorderMapper orderMapper;
	@Resource
	private OrderService orderService;
	@Resource
	private WishService wishService;

	@Override
	public void createExpress(Express express, Long orderid) {
		Toyorder order = orderService.selectOrderDetail(orderid);
		if(order==null)
			throw new ServiceException("");
		express.setOrderId(order.getOrderId());
		express.setUserId(order.getUserId());
		express.setCreatetime(new Date());
		expressMapper.insert(express);
		order.setStatus(ToyConst.TOY_ORDER_STATUS_DELIVER);
		order.setOrderId(order.getOrderId());
		order.setConfirmTime(new Date());
		orderMapper.updateByPrimaryKeySelective(order);
		// 快递成功后变更wish状态
		Wish wish=new Wish();
		wish.setWishId(order.getWishId());
		wish.setStatus(ToyConst.TOY_WISH_STATUS_FINISH);
		wishService.updateWish(wish);
	}

	@Override
	public Express selectExpressByOrderid(Long orderid) {
		ExpressExample expressExample = new ExpressExample();
		Criteria criteria = expressExample.createCriteria();
		criteria.andOrderIdEqualTo(orderid);
		List<Express> expresslist = expressMapper
				.selectByExample(expressExample);
		if (expresslist == null || expresslist.isEmpty()
				)
			return null;
		return expresslist.get(0);

	}

	@Override
	public void createExpressByOrderId(Long orderid) {
		//Order order=
		
	}

}
